package utils

import (
	"fmt"
	"gopkg.in/ini.v1"
)

var (
	Addr             string
	Port             int
	DirPath          string
	BlockSize        int64
	IdxMode          int
	MaxKeySize       uint64
	MaxValueSize     uint64
	Sync             bool
	ReclaimThreshold int
	FileRWMethod     int

	CmdHistoryPath string
)

func init() {
	file, err := ini.Load("D:\\GoProject\\mytinydb\\config\\config.ini")
	if err != nil {
		fmt.Println("配置文件读取错误")
	}
	LoadDb(file)
	LoadCmdHistoryPath(file)
}

func LoadDb(file *ini.File) {
	Addr = file.Section("dbCore").Key("Addr").MustString("127.0.0.1")
	Port = file.Section("dbCore").Key("Port").MustInt(10000)
	DirPath = file.Section("dbCore").Key("DirPath").MustString("../data/server/server_data")
	BlockSize = file.Section("dbCore").Key("BlockSize").MustInt64(16 * 1024 * 1024)
	IdxMode = file.Section("dbCore").Key("IdxMode").MustInt(0)
	MaxKeySize = file.Section("dbCore").Key("MaxKeySize").MustUint64(128)
	MaxValueSize = file.Section("dbCore").Key("MaxValueSize").MustUint64(1 * 1024 * 1024)
	Sync = file.Section("dbCore").Key("Sync").MustBool(false)
	ReclaimThreshold = file.Section("dbCore").Key("ReclaimThreshold").MustInt(4)
	FileRWMethod = file.Section("dbCore").Key("FileRWMethod").MustInt(0)
}

func LoadCmdHistoryPath(file *ini.File) {
	CmdHistoryPath = file.Section("cmdHistoryPath").Key("Path").MustString("../data/client/cmdhistory")
}
